package cn.ht.jywrwater.module.ht.controller.admin.devicedata;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;

import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;

import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ht.jywrwater.module.ht.controller.admin.devicedata.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.devicedata.DeviceDataDO;
import cn.ht.jywrwater.module.ht.convert.devicedata.DeviceDataConvert;
import cn.ht.jywrwater.module.ht.service.devicedata.DeviceDataService;

@Tag(name = "管理后台 - 设备数据")
@RestController
@RequestMapping("/ht/device-data")
@Validated
public class DeviceDataController {

    @Resource
    private DeviceDataService deviceDataService;

    @PostMapping("/create")
    @Operation(summary = "创建设备数据")
    @PreAuthorize("@ss.hasPermission('ht:device-data:create')")
    public CommonResult<Long> createDeviceData(@Valid @RequestBody DeviceDataCreateReqVO createReqVO) {
        return success(deviceDataService.createDeviceData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备数据")
    @PreAuthorize("@ss.hasPermission('ht:device-data:update')")
    public CommonResult<Boolean> updateDeviceData(@Valid @RequestBody DeviceDataUpdateReqVO updateReqVO) {
        deviceDataService.updateDeviceData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:device-data:delete')")
    public CommonResult<Boolean> deleteDeviceData(@RequestParam("id") Long id) {
        deviceDataService.deleteDeviceData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:device-data:query')")
    public CommonResult<DeviceDataRespVO> getDeviceData(@RequestParam("id") Long id) {
        DeviceDataDO deviceData = deviceDataService.getDeviceData(id);
        return success(DeviceDataConvert.INSTANCE.convert(deviceData));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备数据列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:device-data:query')")
    public CommonResult<List<DeviceDataRespVO>> getDeviceDataList(@RequestParam("ids") Collection<Long> ids) {
        List<DeviceDataDO> list = deviceDataService.getDeviceDataList(ids);
        return success(DeviceDataConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备数据分页")
    @PreAuthorize("@ss.hasPermission('ht:device-data:query')")
    public CommonResult<PageResult<DeviceDataRespVO>> getDeviceDataPage(@Valid DeviceDataPageReqVO pageVO) {
        PageResult<DeviceDataDO> pageResult = deviceDataService.getDeviceDataPage(pageVO);
        return success(DeviceDataConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备数据 Excel")
    @PreAuthorize("@ss.hasPermission('ht:device-data:export')")
    @OperateLog(type = EXPORT)
    public void exportDeviceDataExcel(@Valid DeviceDataExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DeviceDataDO> list = deviceDataService.getDeviceDataList(exportReqVO);
        // 导出 Excel
        List<DeviceDataExcelVO> datas = DeviceDataConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备数据.xls", "数据", DeviceDataExcelVO.class, datas);
    }

    @GetMapping("/getValue")
    @Operation(summary = "获得某个设备值")
    @Parameter(name = "deviceCode", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:device-data:query')")
    public CommonResult<DeviceDataRespVO> getDeviceData(@RequestParam("deviceCode") String deviceCode) {
        DeviceDataDO deviceData = deviceDataService.getDeviceValue(deviceCode);
        return success(DeviceDataConvert.INSTANCE.convert(deviceData));
    }

}
